FROM ubuntu:latest
MAINTAINER luotianyiovo <luotianyiovo@gmail.com>

# 安装目录
ARG DEFAULT_DIR="/usr/local/webserver"
ARG DOWNLOAD_DIR="/usr/local/src/download"

# web 应用目录
ARG WEB_APP_DIR="/usr/local/webserver/nginx/web_app"

ARG php_version=8.0.8
ARG nginx_version=1.20.1

ENV DEBIAN_FRONTEND=noninteractive

RUN mkdir $DOWNLOAD_DIR &&  mkdir $DEFAULT_DIR

VOLUME $DEFAULT_DIR $DEFAULT_DIR

RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list && \
    apt-get update && \
    apt-get -y upgrade && \
    apt-get install -y autoconf wget vim build-essential git libxml2-dev pkg-config && \
    apt-get install -y libxml2-dev libcurl3-dev librecode-dev libjpeg-dev libpng-dev \
    libsasl2-2 sasl2-bin libsasl2-2 libsasl2-dev libsasl2-modules libfreetype6-dev \
    libreadline-dev libpcre3-dev libssl-dev libcurl4-openssl-dev libsqlite3-dev \
    libbz2-1.0 libbz2-dev libffi-dev libgmp-dev libonig-dev libxslt-dev libzip-dev

# 安装nginx
RUN mkdir $DOWNLOAD_DIR/nginx && cd $DOWNLOAD_DIR/nginx \
    && wget https://nginx.org/download/nginx-$nginx_version.tar.gz\
    && tar -zvxf nginx-$nginx_version.tar.gz && cd nginx-$nginx_version\
    && ./configure --prefix=$DEFAULT_DIR/nginx --with-http_ssl_module --with-http_v2_module && make && make install\
    && rm -rf $DOWNLOAD_DIR/nginx

# 安装mysql
RUN apt-get install -y mysql-server && \
    mkdir -p /var/run/mysqld && chown mysql:mysql /var/run/mysqld && \
    sed -i 's/^\(log_error\s.*\)/# \1/' /etc/mysql/my.cnf && \
    echo "mysqld_safe &" > /tmp/mysql_config && \
    echo "mysqladmin --silent --wait=30 ping || exit 1" >> /tmp/mysql_config && \
    echo "mysql -e \"ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456'; flush privileges;\"" >> /tmp/mysql_config && \
    bash /tmp/mysql_config && \
    rm -f /tmp/mysql_config

# 安装 PHP
RUN mkdir $DOWNLOAD_DIR/php && cd $DOWNLOAD_DIR/php \
    && wget https://mirrors.sohu.com/php/php-$php_version.tar.gz \
    && tar -zvxf php-$php_version.tar.gz && cd php-$php_version \
    && ./configure --prefix=$DEFAULT_DIR/php \
    --with-config-file-path=$DEFAULT_DIR/php/etc \
    --enable-fpm \
    --enable-inline-optimization \
    --disable-debug \
    --disable-rpath \
    --enable-shared  \
    --with-xmlrpc \
    --with-mhash \
    --with-sqlite3 \
    --with-zlib \
    --enable-bcmath \
    --with-iconv \
    --with-bz2 \
    --with-openssl \
    --enable-calendar \
    --with-curl \
    --with-cdb \
    --enable-dom \
    --enable-exif \
    --enable-fileinfo \
    --enable-filter \
    --with-openssl-dir \
    --with-zlib-dir \
    --enable-gd-jis-conv \
    --with-gettext \
    --with-gmp \
    --with-mhash \
    --enable-json \
    --enable-mbstring \
    --enable-mbregex \
    --enable-pdo \
    --with-mysqli \
    --with-pdo-mysql \
    --with-pdo-sqlite \
    --with-readline \
    --enable-session \
    --enable-shmop \
    --enable-simplexml \
    --enable-sockets  \
    --enable-sysvmsg \
    --enable-sysvsem \
    --enable-sysvshm \
    --with-xsl \
    --enable-mysqlnd-compression-support \
    --with-pear \
    --enable-opcache \
    --with-zip \
    --with-ffi \
     && make && make install \
     && cp php.ini-production $DEFAULT_DIR/php/etc/php.ini \
     && mv $DEFAULT_DIR/php/etc/php-fpm.conf.default $DEFAULT_DIR/php/etc/php-fpm.conf \
     && mv $DEFAULT_DIR/php/etc/php-fpm.d/www.conf.default $DEFAULT_DIR/php/etc/php-fpm.d/www.conf

RUN groupadd nobody && useradd www

EXPOSE 80
EXPOSE 443
EXPOSE 9000
EXPOSE 3306

CMD /usr/local/webserver/php/sbin/php-fpm && /usr/local/webserver/nginx/sbin/nginx && mysqld_safe
